
# null pointer check
cmp $0,%eax
je labelNPE


# array bounds check
mov -4(%eax),%ebx  # ebx = length
mov $0,%ecx        # ecx = index
cmp %ecx,%ebx
jle labelABE       # ebx <= ecx ?
cmp $0,%ecx
jl  labelABE       # ecx < 0 ?



# array size check
cmp $0,%eax		# eax == array size
jle labelASE    # eax <= 0 ?



# division by zero check
cmp $0,%eax	# eax is divisor 
je labelDBE     # eax == 0 ?




#
# =========================================================================
#



.data

    .int 40
strNPE: .string "Runtime error: Null pointer dereference."
    .int 41
strABE: .string "Runtime error: Array index out of bounds!"
    .int 56
strAAE: .string "Runtime error: Array allocation with negative array size!"
    .int 32
strDBE: .string "Runtime error: Division by zero!"

.text

#------------------------- Runtime error handlers -------------------------

.align 4
labelNPE:
    push $strNPE
    call __println
    push $1
    call __exit
	
.align 4
labelABE:
    push $strABE
    call __println
    push $1
    call __exit
	
.align 4
labelASE:
    push $strASE
    call __println
    push $1
    call __exit
	
.align 4
labelDBE:
    push $strDBE
    call __println
    push $1
    call __exit
